﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class programacionController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /programacion/

        public ActionResult Index()
        {
            //string start = Convert.ToString(Request.QueryString["start"]);
            //string end = Convert.ToString(Request.QueryString["end"]);

            int id = 0;
            if (Session["IdPersonal"] != null)
            {
                id = Convert.ToInt32(Session["IdPersonal"].ToString());
            }

            var query = entidades.PAListaActividadPersonal(id).ToList();
            return Json(new { evts = query, total = query.Count() }, JsonRequestBehavior.AllowGet);

        }

        public ActionResult ListaActividad()
        {
            var query = entidades.PAListaActividad().ToList();
            return Json(new { list = query, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        
        public ActionResult ListaActividadPersonal()
        {
            string start = Convert.ToString(Request.QueryString["start"]);
            string end = Convert.ToString(Request.QueryString["end"]);

            int id = Session["IdPersonal"] != null ? Convert.ToInt32(Session["IdPersonal"].ToString()) : 0;

            var query = entidades.PAListaActividadPersonal(id).ToList();
            return Json(new { evts = query, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult Guardar(FormCollection collection)
        {
            try
            {
                int idpersonal = 0;
                if (Session["IdPersonal"] != null)
                {
                    idpersonal = Convert.ToInt32(Session["IdPersonal"].ToString());
                }
                else
                {
                    return Content("Error de Sesion");
                }

                int nuevo = Convert.ToInt32(collection["nuevo"].ToString());
                int idactividadpersonal = Convert.ToInt32(collection["idactividadpersonal"].ToString());
                int idactividad = Convert.ToInt32(collection["idactividad"].ToString());
                DateTime fecinicio = Convert.ToDateTime(collection["fecinicio"].ToString());
                DateTime fecfin = Convert.ToDateTime(collection["fecfin"].ToString());
                int tododia = Convert.ToInt32(collection["tododia"].ToString());
                string lugar = Convert.ToString(collection["lugar"].ToString());
                string valor = Convert.ToString(collection["valor"].ToString());
                int programado = Convert.ToInt32(collection["programado"].ToString());
                int ejecutado = Convert.ToInt32(collection["ejecutado"].ToString());
                string sustento = Convert.ToString(collection["sustento"].ToString());
                
                entidades.PAGuardarActividadPersonal(idpersonal, nuevo, idactividadpersonal, idactividad, fecinicio, fecfin, tododia, lugar, valor, programado, ejecutado, sustento);

                return Content("OK");
            }
            catch
            {
                return Content("Error de App");
            }
        }

        [HttpPost]
        public ActionResult Eliminar(FormCollection collection)
        {
            try
            {
                int idpersonal = 0;
                if (Session["IdPersonal"] != null)
                {
                    idpersonal = Convert.ToInt32(Session["IdPersonal"].ToString());
                }
                else
                {
                    return Content("Error de Sesion");
                }
                int idactividadpersonal = 0;
                idactividadpersonal = Convert.ToInt32(collection["idactividadpersonal"]);
                if (idactividadpersonal == 0)
                {
                    return Content("Error de Datos");
                }
                entidades.PAEliminarActividadPersonal(idactividadpersonal);
                return Content("OK");
            }
            catch
            {
                return Content("Error de App");
            }
        }
    }
}
